package org.backend.controller;

import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.backend.annotation.LoginOperation;
import org.backend.pojo.entity.WxUser;
import org.backend.pojo.dto.LoginRequestDTO;
import org.backend.pojo.dto.WxUserLoginDTO;
import org.backend.pojo.entity.User;
import org.backend.pojo.vo.LoginResponseVO;
import org.backend.pojo.vo.WxUserInfoVO;
import org.backend.pojo.vo.WxUserLoginVO;
import org.backend.service.UserService;
import org.backend.utils.ApiResponse;
import org.backend.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    @LoginOperation
    public ApiResponse<LoginResponseVO> login(@RequestBody LoginRequestDTO loginRequest) {
        log.info("用户登录信息：{}", loginRequest);
        LoginResponseVO response = userService.login(loginRequest.getAccount(), loginRequest.getPassword());
        if (response != null) {
            return ApiResponse.success(response);
        } else {
            return  ApiResponse.error("登录失败");
        }
    }

    @PostMapping("/register")
    public ApiResponse<String> register(@RequestBody User user) {
        return userService.register(user);
    }

    @PostMapping("/saveUserInfo")
    public ApiResponse<String> saveUserInfo(@RequestBody User user) {
        System.out.println(user);
        return userService.saveUserInfo(user);
    }

    @PostMapping("/wxlogin")
    public ApiResponse<WxUserLoginVO> wxLogin(@RequestBody WxUserLoginDTO wxUserLoginDTO) {
        log.info("微信用户登录:{}", wxUserLoginDTO.getCode());

        // 微信登录
        WxUser wxUser = userService.wxLogin(wxUserLoginDTO);

        // 为微信用户生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put("openid", wxUser.getOpenid());

        String token = JwtUtil.generateToken(claims);

        WxUserLoginVO userLoginVO = WxUserLoginVO.builder()
                .openid(wxUser.getOpenid())
                .token(token)
                .build();
        log.info("返回的对象是:{}", userLoginVO);
        return ApiResponse.success(userLoginVO);
    }

    /**
     * 微信小程序获取个人信息
     * @param request
     * @return
     */
    @PostMapping("/getWxUserInfo")
    public ApiResponse<WxUserInfoVO> getWxUserInfo(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (token != null) {
            try {
                Claims claims = JwtUtil.parseToken(token);
                String openId = (String) claims.get("openid");
                return userService.getWxUserInfo(openId);
            } catch (Exception e) {
                log.error("getWxUserInfo: {}", e.getMessage());
            }
        }
        return ApiResponse.error("获取失败");
    }

    @PostMapping("/editWxUserInfo")
    public ApiResponse<String> editWxUserInfo(@RequestBody WxUserInfoVO wxUserInfoVO) {
        return userService.editWxUserInfo(wxUserInfoVO);
    }

}
